{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting .\\train-images-idx3-ubyte.gz\n",
      "Extracting .\\train-labels-idx1-ubyte.gz\n",
      "Extracting .\\t10k-images-idx3-ubyte.gz\n",
      "Extracting .\\t10k-labels-idx1-ubyte.gz\n",
      "##########\n",
      "step 100, entropy loss: 0.3331621289253235, l2_loss:131.877197265625, total loss:0.34239354729652405\n",
      "0.96\n",
      "0.9081\n",
      "##########\n",
      "step 200, entropy loss: 0.295483261346817, l2_loss:150.96697998046875, total loss:0.30605095624923706\n",
      "0.95\n",
      "0.9269\n",
      "##########\n",
      "step 300, entropy loss: 0.1465371549129486, l2_loss:166.12425231933594, total loss:0.1581658571958542\n",
      "0.99\n",
      "0.9411\n",
      "##########\n",
      "step 400, entropy loss: 0.19084596633911133, l2_loss:179.40966796875, total loss:0.20340465009212494\n",
      "0.98\n",
      "0.9517\n",
      "##########\n",
      "step 500, entropy loss: 0.21331287920475006, l2_loss:191.4749755859375, total loss:0.22671613097190857\n",
      "0.98\n",
      "0.9584\n",
      "##########\n",
      "step 600, entropy loss: 0.141788050532341, l2_loss:201.0340118408203, total loss:0.15586042404174805\n",
      "0.98\n",
      "0.9544\n",
      "##########\n",
      "step 700, entropy loss: 0.10874442756175995, l2_loss:209.6658935546875, total loss:0.12342104315757751\n",
      "1.0\n",
      "0.9637\n",
      "##########\n",
      "step 800, entropy loss: 0.10167071223258972, l2_loss:218.2113037109375, total loss:0.11694550514221191\n",
      "1.0\n",
      "0.9671\n",
      "##########\n",
      "step 900, entropy loss: 0.08533880859613419, l2_loss:227.17762756347656, total loss:0.10124124586582184\n",
      "1.0\n",
      "0.968\n",
      "##########\n",
      "step 1000, entropy loss: 0.08461733162403107, l2_loss:234.5009765625, total loss:0.10103239864110947\n",
      "1.0\n",
      "0.9676\n",
      "##########\n",
      "step 1100, entropy loss: 0.09005243331193924, l2_loss:235.81781005859375, total loss:0.10655967891216278\n",
      "1.0\n",
      "0.974\n",
      "##########\n",
      "step 1200, entropy loss: 0.06519537419080734, l2_loss:238.20376586914062, total loss:0.08186963945627213\n",
      "1.0\n",
      "0.9751\n",
      "##########\n",
      "step 1300, entropy loss: 0.06138979271054268, l2_loss:240.31515502929688, total loss:0.07821185141801834\n",
      "1.0\n",
      "0.9759\n",
      "##########\n",
      "step 1400, entropy loss: 0.025501057505607605, l2_loss:242.20045471191406, total loss:0.04245509207248688\n",
      "1.0\n",
      "0.9767\n",
      "##########\n",
      "step 1500, entropy loss: 0.06135587766766548, l2_loss:243.8892059326172, total loss:0.07842811942100525\n",
      "0.99\n",
      "0.977\n",
      "##########\n",
      "step 1600, entropy loss: 0.029840314760804176, l2_loss:245.707763671875, total loss:0.047039858996868134\n",
      "1.0\n",
      "0.9775\n",
      "##########\n",
      "step 1700, entropy loss: 0.026991980150341988, l2_loss:247.8861541748047, total loss:0.044344011694192886\n",
      "1.0\n",
      "0.979\n",
      "##########\n",
      "step 1800, entropy loss: 0.06093999370932579, l2_loss:250.1746368408203, total loss:0.0784522145986557\n",
      "1.0\n",
      "0.9782\n",
      "##########\n",
      "step 1900, entropy loss: 0.028231512755155563, l2_loss:252.37841796875, total loss:0.04589800164103508\n",
      "1.0\n",
      "0.9783\n",
      "##########\n",
      "step 2000, entropy loss: 0.06915261596441269, l2_loss:254.1991729736328, total loss:0.08694656193256378\n",
      "1.0\n",
      "0.9772\n",
      "##########\n",
      "step 2100, entropy loss: 0.03756890445947647, l2_loss:254.31781005859375, total loss:0.05537115037441254\n",
      "0.99\n",
      "0.9794\n",
      "##########\n",
      "step 2200, entropy loss: 0.028124049305915833, l2_loss:254.5225067138672, total loss:0.04594062268733978\n",
      "1.0\n",
      "0.9798\n",
      "##########\n",
      "step 2300, entropy loss: 0.04819000884890556, l2_loss:254.83551025390625, total loss:0.06602849066257477\n",
      "0.99\n",
      "0.9799\n",
      "##########\n",
      "step 2400, entropy loss: 0.034738533198833466, l2_loss:255.15487670898438, total loss:0.05259937420487404\n",
      "0.99\n",
      "0.9792\n",
      "##########\n",
      "step 2500, entropy loss: 0.03061930648982525, l2_loss:255.39572143554688, total loss:0.04849700629711151\n",
      "1.0\n",
      "0.9797\n",
      "##########\n",
      "step 2600, entropy loss: 0.017940308898687363, l2_loss:255.69947814941406, total loss:0.035839274525642395\n",
      "1.0\n",
      "0.9798\n",
      "##########\n",
      "step 2700, entropy loss: 0.03233572468161583, l2_loss:256.0111389160156, total loss:0.050256505608558655\n",
      "1.0\n",
      "0.9801\n",
      "##########\n",
      "step 2800, entropy loss: 0.04979501664638519, l2_loss:256.3280944824219, total loss:0.06773798167705536\n",
      "0.98\n",
      "0.9803\n",
      "##########\n",
      "step 2900, entropy loss: 0.058491241186857224, l2_loss:256.5689697265625, total loss:0.07645107060670853\n",
      "0.98\n",
      "0.9792\n",
      "##########\n",
      "step 3000, entropy loss: 0.003435192396864295, l2_loss:256.8783264160156, total loss:0.02141667529940605\n",
      "1.0\n",
      "0.9793\n",
      "##########\n",
      "step 3100, entropy loss: 0.023738862946629524, l2_loss:256.9986267089844, total loss:0.04172876477241516\n",
      "1.0\n",
      "0.9793\n",
      "##########\n",
      "step 3200, entropy loss: 0.007176213897764683, l2_loss:257.1140441894531, total loss:0.025174196809530258\n",
      "1.0\n",
      "0.9799\n",
      "##########\n",
      "step 3300, entropy loss: 0.04022635892033577, l2_loss:257.24847412109375, total loss:0.058233752846717834\n",
      "0.99\n",
      "0.9806\n",
      "##########\n",
      "step 3400, entropy loss: 0.01881466992199421, l2_loss:257.36883544921875, total loss:0.036830488592386246\n",
      "1.0\n",
      "0.9806\n",
      "##########\n",
      "step 3500, entropy loss: 0.08117739856243134, l2_loss:257.52362060546875, total loss:0.09920404851436615\n",
      "0.98\n",
      "0.98\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "mnist = input_data.read_data_sets('.', one_hot=True)   #one_hot 表示是否展开独热编码的值\n",
    "\n",
    "def activation(x):\n",
    "    return x*tf.nn.sigmoid(x)\n",
    "\n",
    "\n",
    "learning_rate = tf.placeholder(tf.float32)\n",
    "\n",
    "#定义单层网络\n",
    "#输入数据\n",
    "#mnist 数据集的图像大小为28*28，应该是二维，但是mnist按照一维进行存储,由于不确定一共有多少数据，第1维因此为None\n",
    "x = tf.placeholder(tf.float32, [None, 784], name='x') \n",
    "\n",
    "#第1个隐层784*100\n",
    "W1 = tf.Variable(tf.truncated_normal([784, 100], stddev=0.05),name='weight1')    \n",
    "#偏置\n",
    "b1 = tf.Variable(tf.zeros([100], name='bias1'))\n",
    "logits1 = tf.matmul(x, W1) + b1\n",
    "output1 = activation(logits1)\n",
    "\n",
    "#第2个隐层100*10\n",
    "W2 = tf.Variable(tf.truncated_normal([100, 10], stddev=0.063),name='weight2')\n",
    "b2 = tf.Variable(tf.zeros([10], name='bias2'))\n",
    "# 最终未经激活的输出\n",
    "logits2 = tf.matmul(output1, W2) + b2  #matmul矩阵乘法\n",
    "\n",
    "y = tf.placeholder(tf.float32, [None, 10])\n",
    "\n",
    "#使用交叉熵损失+L2正则\n",
    "cross_entropy = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=logits2)) \n",
    "l2_loss = tf.nn.l2_loss(W1) + tf.nn.l2_loss(W2)\n",
    "total_loss = cross_entropy + 7e-5*l2_loss\n",
    "#定义优化器,采用梯度下降优化器\n",
    "train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(total_loss)\n",
    "\n",
    "#tf.arg_max()用于获取某一维数据最大值的索引，第二个参数指定了维度\n",
    "correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(logits2, 1))\n",
    "#计算准确率\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "\n",
    "sess = tf.Session()\n",
    "sess.run(tf.global_variables_initializer())\n",
    "\n",
    "lr = 1.0\n",
    "for step in range(3500):\n",
    "    if step < 1000:\n",
    "        lr = 1.0\n",
    "    elif step < 2000:\n",
    "        lr = 0.5\n",
    "    elif step < 3000:\n",
    "        lr = 0.1\n",
    "    else:\n",
    "        lr = 0.05\n",
    "        \n",
    "    batch_x, batch_y = mnist.train.next_batch(100)\n",
    "    _, loss, l2_loss_value, total_loss_value = sess.run(\n",
    "        [train_step, cross_entropy, l2_loss, total_loss], \n",
    "        feed_dict={x:batch_x, y:batch_y, learning_rate:lr})\n",
    "    if(step+1) % 100 == 0:\n",
    "        print('#' * 10)\n",
    "        print(\"step {}, entropy loss: {}, l2_loss:{}, total loss:{}\".format(step + 1, loss, l2_loss_value, total_loss_value))\n",
    "        print(sess.run(accuracy, feed_dict={x:batch_x, y:batch_y}))\n",
    "        print(sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels}))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
